package com.xja.dubbo.service.impl;

import com.xja.dubbo.entity.Easybuy_user;
import com.xja.dubbo.entity.Easybuy_user_address;
import com.xja.dubbo.entity.Easybuy_vipuser;
import com.xja.dubbo.mapper.Easybuy_userMapper;
import com.xja.dubbo.mapper.Easybuy_user_addressMapper;
import com.xja.dubbo.mapper.Easybuy_vipuserMapper;
import com.xja.dubbo.service.UserService;
import com.xja.dubbo.util.DateUtil;
import com.xja.dubbo.util.JsonUtil;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author ycz
 * @create 2021-12-18-9:22
 */
@Component
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService{


    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private Easybuy_userMapper easybuy_userMapper;

    @Autowired
    private Easybuy_user_addressMapper easybuy_user_addressMapper;

    @Autowired
    private Easybuy_vipuserMapper easybuy_vipuserMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public Easybuy_user findUserByName(String name) throws Exception {
        Easybuy_user easybuy_user = easybuy_userMapper.findUserByName(name);

        return easybuy_user;
    }

    @Override
    public void saveLoginUserToRedis(Easybuy_user user, String uuid) throws Exception {
          logger.info("把用户保存到redis中");
         String userStr = JsonUtil.objectToJson(user);
         redisTemplate.boundValueOps("loginuser_"+uuid).set(userStr);
         redisTemplate.boundValueOps("loginuser_"+uuid).expire(5, TimeUnit.MINUTES);

    }

    @Override
    public Easybuy_user findLoginUserFromRedis(String uuid) throws Exception {
        String userStr = redisTemplate.boundValueOps("loginuser_"+uuid).get();
        redisTemplate.boundValueOps("loginuser_"+uuid).expire(5, TimeUnit.MINUTES);
        return (Easybuy_user) JsonUtil.jsonToPojo(userStr, Easybuy_user.class);

    }

    @Override
    public List<Easybuy_user_address> selectAddressById(Integer uid) throws Exception {
        List<Easybuy_user_address> userAddresses = easybuy_user_addressMapper.selectAddressById(uid);
        return userAddresses;
    }

    @Override
    public int selectVipUser(Integer userId) throws Exception {
        String ym = DateUtil.getDateNow();
        return easybuy_vipuserMapper.selectVipUser(userId, ym);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void insertNewAddress(Easybuy_user_address easybuy_user_address) throws Exception {
           logger.info("添加新地址");
        Easybuy_user_address userAddress = easybuy_user_addressMapper.selectAddressByAddress(easybuy_user_address.getAddress());

        if(userAddress==null || "".equals(userAddress)){
            easybuy_user_addressMapper.insertSelective(easybuy_user_address);

        }


    }
}
